package com.example.meterialproject.view.behavior.demo2

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView
import com.example.meterialproject.exts.marginBottomAndTop
import com.example.meterialproject.exts.name

/**
 *
 * @ClassName: BottomNavigationBehavior
 * @Author: android 超级兵
 * @CreateDate: 4/15/22$ 12:59 PM$
 * TODO
 */
class BottomNavigationBehavior(val context: Context, attrs: AttributeSet?) :
    CoordinatorLayout.Behavior<View>(context, attrs) {
    companion object {
        private const val TAG = "BNBehavior"
    }

    override fun layoutDependsOn(
        parent: CoordinatorLayout,
        child: View,
        dependency: View
    ): Boolean {
        Log.i(TAG, "layoutDependsOn dependency:${dependency.name}")
        return dependency is RecyclerView
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 4/24/22 3:37 PM
     * TODO 允许嵌套滑动
     */
    override fun onStartNestedScroll(
        coordinatorLayout: CoordinatorLayout,
        child: View,
        directTargetChild: View,
        target: View,
        axes: Int,
        type: Int
    ): Boolean {
        return true
    }

    override fun onLayoutChild(
        parent: CoordinatorLayout,
        child: View,
        layoutDirection: Int
    ): Boolean {
        parent.onLayoutChild(child, layoutDirection)
        originChildTop = child.top
        originChildHeight = child.height * 3 / 2
        Log.i(TAG, "originChildTop:$originChildTop\toriginChildHeight:${originChildHeight}")
        return true
    }

    // 原始bottomTop位置
    private var originChildTop = 0

    // 记录 原始 3 / 2 的高度
    private var originChildHeight = 0

    override fun onNestedPreScroll(
        coordinatorLayout: CoordinatorLayout,
        child: View,
        target: View,
        dx: Int,
        dy: Int,
        consumed: IntArray,
        type: Int
    ) {

        // 最大滑动距离
        val maxScrollY = originChildTop + child.height + child.marginBottomAndTop

        // 最小滑动距离
        val minScrollY = originChildTop

        Log.i(
            TAG,
            "maxScrollY${maxScrollY}\tminScrollY:$minScrollY\tdy:$dy"
        )
        // 当前滑动距离
        val currentScrollY = child.top
//
        val up = dy > 0 && currentScrollY < maxScrollY
        val down = dy < 0 && currentScrollY > minScrollY - originChildHeight
        if (up || down) {
            // 偏移 bottomNavigationView
            ViewCompat.offsetTopAndBottom(child, dy)
        }

        // 避免快速滑动冲突
        if (currentScrollY > maxScrollY) {
            child.y = maxScrollY
        }

        if (currentScrollY < minScrollY) {
            child.y = minScrollY.toFloat()
        }
    }
}